<?php
	// 未登录不允许发表博客
session_start();
$user = isset($_SESSION['user'])?$_SESSION['user']:false;
if(!$user){
	exit('您还未登录，请登录后再发表博客');
}

require_once $_SERVER['DOCUMENT_ROOT'].'/myblog1/lib/Db.php';
$db = new Db();

$cates = $db->table('cates')->lists();

?>

<!DOCTYPE html>
<html>
<head>
	<title>发表博客</title>
	<link rel="stylesheet" type="text/css" href="./static/plugins/bootstrap/css/bootstrap.min.css">
	<script type="text/javascript" src="./static/js/jquery.min.js"></script>
	<script type="text/javascript" src="./static/plugins/bootstrap/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="./static/js/util.js"></script>
	<script type="text/javascript" src="./static/plugins/wangEditor.min.js"></script>
</head>
<body>
	
	<div class="form-horizontal">
	  <div class="form-group">
	    <label for="inputEmail3" class="col-xs-2 control-label">博客标题</label>
	    <div class="col-xs-10">
	      <input type="text" class="form-control" id="title" placeholder="请输入博客的标题">
	    </div>
	  </div>
	  <div class="form-group">
	    <label for="inputPassword3" class="col-xs-2 control-label">博客分类</label>
	    <div class="col-xs-10">
	      	<select class="form-control" id="cid">
	      		<?php foreach($cates as $cate){?>
				<option value="<?php echo $cate['id']?>"><?php echo $cate['title']?></option>
				<?php }?>
			</select>
	    </div>
	  </div>

	  <div class="form-group">
	    <label for="inputEmail3" class="col-xs-2 control-label">博客内容</label>
	    <div class="col-xs-10">
	      	<div id="editor"></div>
	    </div>
	  </div>

	  <div class="form-group">
	    <div class="col-xs-offset-2 col-xs-10">
	      <button type="submit" class="btn btn-primary btn-lg btn-block" onclick="save();">保存</button>
	    </div>
	  </div>
	</div>
</body>
</html>

<script type="text/javascript">
	
	var editor;
	// 初始化
	function init(){
		var E = window.wangEditor;
		editor = new E('#editor');
		editor.customConfig.showLinkImg = false;
		editor.customConfig.uploadImgServer = '/upload.php';
		editor.create();
	}
    init();


    // 发表博客
    function save(){
    	var title = $('#title').val();
		var cid = $('#cid').val();
		var msg = editor.txt.html();
		if(title==''){
			UI.alert({msg:'请输入博客的标题',icon:'error'});
			return;
		}
		if(msg=='' || msg=='<p><br></p>'){
			UI.alert({msg:'请输入博客内容',icon:'error'});
			return;
		}
		$.post('/myblog1/service/save_article.php',{title:title,cid:cid,msg:msg},function(res){
			if(res.code>0){
				UI.alert({msg:res.msg,icon:'error'});
			}else{
				UI.alert({msg:res.msg,icon:'ok'});
				setTimeout(function(){window.location.reload();},1000);
			}
		},'json');
    }
</script>